package pro.softzhang.algo.lc1400;

import pro.softzhang.algo.flag.knowledge.KSlidingWindow;
import pro.softzhang.algo.flag.theme.TVowels;

/**
 * 定长滑动窗口
 */
@KSlidingWindow
@TVowels
public class LC1456_MaximumNumberOfVowelsInASubstringOfGivenLength {
    public static void main(String[] args) {

    }

    /**
     *
     */
    static
    class Solution {
        public int maxVowels(String s, int k) {
            int max = 0;
            for(int i = 0; i < k; i++) {
                max += isVowel(s.charAt(i));
            }
            int winMax = max;
            for(int i = k; i < s.length(); i++) {
                winMax = winMax - isVowel(s.charAt(i - k)) + isVowel(s.charAt(i));
                max = Math.max(max, winMax);
            }
            return max;
        }

        private int isVowel(char ch) {
            return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ? 1 : 0;
        }
    }

    /**
     *
     */
    static
    class Solution1 {
        public int maxVowels(String s, int k) {
            char[] chars = s.toCharArray();

            int max = 0, vowels = 0;
            for (int i = 0; i < chars.length; i++) {
                // 1 “进入”窗口
                if (isVowel(chars[i])) {
                    vowels++;
                }

                // 窗口内容不足K时，不出
                if (i < k - 1) {
                    continue;
                }

                // 2 “更新”最大值
                max = Math.max(max, vowels);

                // 3 离开窗口
                if (isVowel(chars[i - k + 1])) {
                    vowels--;
                }
            }

            return max;
        }

        private boolean isVowel(char ch) {
            return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
        }
    }
}
